package com.example.admin.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Admin;
import exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import com.example.admin.mapper.AdminMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import request.BaseRequest;
import request.LoginRequest;
import request.PasswordRequest;
import request.RegisterRequest;
import com.example.admin.service.IAdminService;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class AdminService implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;


    private static final String DEFAULT_PASS = "123";
    private static final String PASS_SALT = "zap";


    @Override
    public List<Admin> list() {
        return adminMapper.list();
    }

    @Override
    public PageInfo<Admin> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<Admin> users = adminMapper.listByCondition(baseRequest);
        return new PageInfo<>(users);
    }

//    @Override
//    public void save(Admin obj) {
//        // 默认密码 123
//        if (StrUtil.isBlank(obj.getPassword())) {
//            obj.setPassword(DEFAULT_PASS);
//        }
//        obj.setPassword(securePass(obj.getPassword()));  // 设置md5加密
//        try {
//            adminMapper.save(obj);
//        } catch (DuplicateKeyException e) {
//            log.error("数据插入失败， username:{}", obj.getUsername(), e);
//            throw new ServiceException("用户名重复");
//        }
//    }

    @Override
    public void save( RegisterRequest registerRequest) {
        Admin admin = new Admin();
        admin.setCreatetime(new Date());
        admin.setUpdatetime(new Date());
        admin.setStatus(true);
        // 密码加密
        String password = SaSecureUtil.md5(registerRequest.getPassword());
        registerRequest.setPassword(password);

        BeanUtils.copyProperties(registerRequest, admin);
        adminMapper.save(admin);
    }

    @Override
    public void register(RegisterRequest registerRequest) {
        Admin admin = new Admin();
        admin.setCreatetime(DateTime.now());
        admin.setUpdatetime(DateTime.now());
        admin.setStatus(true);
        registerRequest.setPassword(securePass(registerRequest.getPassword()));
        BeanUtils.copyProperties(registerRequest, admin);
        adminMapper.register(admin);
    }


    @Override
    public Admin getById(Integer id) {
        return adminMapper.getById(id);
    }

    @Override
    public void update(Admin user) {
        user.setUpdatetime(new Date());
        adminMapper.updateById(user);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

//    @Override
//    public LoginDTO login(LoginRequest request) {
//        Admin admin = null;
//        try {
//            admin = adminMapper.getByUsername(request.getUsername());
//        } catch (Exception e) {
//            log.error("根据用户名{} 查询出错", request.getUsername());
//            throw new ServiceException("用户名错误");
//        }
//        if (admin == null) {
//            throw new ServiceException("用户名或密码错误");
//        }
//        // 判断密码是否合法
//        String securePass = securePass(request.getPassword());
//        if (!securePass.equals(admin.getPassword())) {
//            throw new ServiceException("用户名或密码错误");
//        }
//        if (!admin.isStatus()) {
//            throw new ServiceException("当前用户处于禁用状态，请联系管理员");
//        }
//        LoginDTO loginDTO = new LoginDTO();
//        BeanUtils.copyProperties(admin, loginDTO);
//
//        // 生成token
//        String token = TokenUtils.genToken(String.valueOf(admin.getId()), admin.getPassword(), 15);
//        loginDTO.setToken(token);
//        return loginDTO;
//    }

    @Override
    public Boolean checkLogin(LoginRequest loginRequest) {
        Admin admin = adminMapper.getByUsername(loginRequest.getUsername());
        return admin != null && admin.getPassword().equals(SaSecureUtil.md5(loginRequest.getPassword()));
    }

    @Override
    public void changePass(PasswordRequest request) {
        // 注意 你要对新的密码进行加密
        request.setNewPass(securePass(request.getNewPass()));
        int count = adminMapper.updatePassword(request);
        if (count <= 0) {
            throw new ServiceException("修改密码失败");
        }
    }

    @Override
    public Admin getByUsername(String username) {
        return null;
    }

    private String securePass(String password) {
        return SecureUtil.md5(password + PASS_SALT);
    }


}
